<?php

class payments_View extends View {
     
    public function createView($action, $params) {
        switch($action) {
            case 'chooseUserError':
                unset($params['uid']);
                unset($params['action']);
                $this->view = $this->errorInfo("Nie udało się pokazać szczegółów dla wybranego użytkownika.");
            case 'menu':
                $f = new Form($this->createLink($params), 'get');
                    $f->addSection("Wybór użytkownika",'chooseUserForm');
                        $f->section()->add(new PureHtml('<span>Wpisz przynajmniej 3 znaki imienia, nazwiska lub loginu użytkownika</span>'));
                        $f->section()->add(new Input('','text','','','','','','user'));
                        $f->section()->add(new PureHtml($this->hiddenInputs($params)));
                        $f->section()->add(new Input('','hidden','uid','0','','','','uid'));
                    $f->addButton(new Input('','submit','','Pokaż szczegóły','','','','showDetails'));
                    $f->addButton(new Input('','button','','Anuluj','','','onClick="location.href= location.href.split(\'?\')[0];"'));
                $this->view .= $f->get();
            break;
        
            case 'showUserInfo':
                $user = $params['user'];
                unset($params['user']);
                $this->view .= '<center><b>Pracownik: ' . $user->getName() . ' ' . $user->getSurname() . ' (' . $user->getLogin() . ')</b></center>';
                $this->view .= '<span class="info">Imię:</span>' . $user->getName() . '<br />';
                $this->view .= '<span class="info">Nazwisko:</span>' . $user->getSurname() . '<br />';
                $this->view .= '<span class="info">Login:</span>' . $user->getLogin() . '<br />';
                $group = new Group((int)$user->getGid());
                $this->view .= '<span class="info">Grupa:</span>' . $group->name . '<br />';
                $this->view .= '<span class="info">Status:</span>' . ($user->getEnabled() ? 'aktywnny' : 'nieaktywny') . '<br />';
                $this->view .= '<span class="info">Ostatnie logowanie:</span>' . $user->getLastLogin() . '<br />';
                $this->view .= '<span class="info">Data założenia konta:</span>' . $user->getDateCreated() . '<br />';
                $salary = new Salary($user);
                $rates = $salary->getRates();
                $hoursWeekly = $salary->getMaxHoursWeekly();
                $this->view .= '<span class="info">Tygodniowa liczba godzin:</span>' . $hoursWeekly . '<br />';
                $this->view .= '<span class="info">Stawka zwykła:</span>' . $rates->normal_rate . '<br />';
                $this->view .= '<span class="info">Stawka za nadgodziny:</span>' . $rates->overhours_rate . '<br />';
                $this->view .= '<span class="info">Stawka za dni świąteczne:</span>' . $rates->weekend_rate . '<br />';
                $this->view .= '<span class="info">Stawka za delegacje:</span>' . $rates->trip_rate . '<br />';
                $buttons = array();
                $buttons[] = array('action'=>'money','text'=>'Zmień stawki');
                $buttons[] = array('action'=>'hours','text'=>'Ustal liczbę godzin');
                $buttons[] = array('action'=>'payment','text'=>'Rozlicz');
                $buttons[] = array('action'=>'','text'=>'Powrót');
                $this->view .= Menu::createMenu($buttons, $params, false, array(2,2), true);
            break;
            
            case 'setUserRatesError':
                $this->view .= $this->errorInfo("Nie udało się ustawić stawek dla użytkownika.");
            case 'moneyInfo':
                $user = $params['user'];
                unset($params['user']);
                $salary = new Salary($user);
                $rates = $salary->getRates();
                $this->view .= '<center><b>Ustawianie stawek dla użytkownika ' . $user->getName() . ' ' . $user->getSurname() . ' (' . $user->getLogin() . ')</b></center>';
                $f = new Form(View::createLink($params), 'post');
                    $f->addSection('Informacje o stawkach', 'ratesInfo');
                        $f->section()->add(new Input('Stawka zwykła:', 'text', 'rates[normal_rate]', (String)$rates->normal_rate, '', '', '', 'normal'));
                        $f->section()->add(new Input('Stawka za nadgodziny:', 'text', 'rates[overhours_rate]', (String)$rates->overhours_rate, '', '', '', 'overhours'));
                        $f->section()->add(new Input('Stawka za dni świąteczne:', 'text', 'rates[weekend_rate]', (String)$rates->weekend_rate, '', '', '', 'weekend'));
                        $f->section()->add(new Input('Stawka za delegacje:', 'text', 'rates[trip_rate]', (String)$rates->trip_rate, '', '', '', 'trip'));
                    $f->addButton(new Input('', 'submit', '', 'Ustaw'));
                    $f->addButton(new Input('', 'button', '', 'Anuluj', '', '', 'onClick="location.href = location.href.replace(\'&action=money\',\'\');"'));
                $this->view .= $f->get();
            break;
            case 'setUserRatesSuccess':
                $this->view = '<script type="text/javascript" language="javascript">
                                    location.href = location.href.replace(\'&action=money\',\'\');
                                </script>';
            break;
        
        
            case 'setUserHoursError':
                $this->view .= $this->errorInfo("Nie udało się ustawić stawek dla użytkownika.");
            case 'hours':
                $user = $params['user'];
                unset($params['user']);
                $salary = new Salary($user);
                $hours = $salary->getMaxHoursWeekly();
                $this->view .= '<center><b>Ustawianie maksymalnej liczby godzin tygodniowo dla użytkownika ' . $user->getName() . ' ' . $user->getSurname() . ' (' . $user->getLogin() . ')</b></center>';
                $f = new Form(View::createLink($params), 'post');
                    $f->addSection('Informacje o liczbie godzin', 'hoursInfo');
                        $f->section()->add(new Input('Liczba godzin:', 'text', 'hours', $hours , '', '', '', 'normal'));
                    $f->addButton(new Input('', 'submit', '', 'Ustaw'));
                    $f->addButton(new Input('', 'button', '', 'Anuluj', '', '', 'onClick="location.href = location.href.replace(\'&action=hours\',\'\');"'));
                $this->view .= $f->get();
            break;
            case 'setUserHoursSuccess':
                $this->view = '<script type="text/javascript" language="javascript">
                                    location.href = location.href.replace(\'&action=hours\',\'\');
                                </script>';
            break;

            case 'payment':
                $user = $params['user'];
                unset($params['user']);
                $salary = new Salary($user);
                $rates = $salary->getRates();
                $date = date('Y-m-d');
                $salary->calculateSalary(date('Y-m-d'));
                $salaryInfo = $salary->getSalaryInfo();
                $total = $salary->getSalaryTotal();
                
                $startDate = date("Y-m", strtotime($date)) . '-01';
                $date = date('Y-m-d',strtotime($startDate . ' next month'));
                $date = date("Y-m", strtotime($date)) . '-01';
                $endDate = date('Y-m-d', strtotime($date)-1);
                
                $work = new Work();
                $worksDone = $work->getHoursByTasks($startDate, $endDate, $user);
                
                $table = '<table class="moneyInfo">
                            <thead>
                                <tr><th colspan="3">Wykonane prace</th></tr>
                                <tr>
                                    <th>Projekt</th>
                                    <th>Zadanie</th>
                                    <th>Czas</th>
                                </tr>
                            </head>
                            <tbody>';
                foreach($worksDone as $key => $hoursDone) {
                    $table .= '<tr>';
                    $task = new ProjectTask((int)$hoursDone['ptid']);
                    $project = new Project((int)$task->getPid());
                    $table .= '<td>' . $project->name . '</td>';
                    $table .= '<td>' . $task->name . '</td>';
                    $table .= '<td>' . $hoursDone['hours'] .  '</td>';
                    $table .= '</tr>';
                }
                $table .= '</tbody></table>';
                $this->view .= '<center><b>Rozliczenie pracownika ' . $user->getName() . ' ' . $user->getSurname() . ' (' . $user->getLogin() . ')</b></center><br />';
                $this->view .= $table;
                $table = '<table class="moneyInfo">
                                <thead>
                                    <tr>
                                        <th colspan="3">Przepracowane godziny</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td>Podstawowe</td>
                                        <td>' . $salaryInfo['normal'] . ' x ' . $rates->normal_rate . '</td>
                                        <td>' . ($salaryInfo['normal'] * $rates->normal_rate) . '</td>
                                    </tr>
                                    <tr>
                                        <td>Nadgodziny</td>
                                        <td>' . $salaryInfo['overhours'] . ' x ' . $rates->overhours_rate . '</td>
                                        <td>' . ($salaryInfo['overhours'] * $rates->overhours_rate) . '</td>
                                    </tr>
                                    <tr>
                                        <td>Weekendy</td>
                                        <td>' . $salaryInfo['weekends'] . ' x ' . $rates->weekend_rate . '</td>
                                        <td>' . ($salaryInfo['weekends'] * $rates->weekend_rate) . '</td>
                                    </tr>
                                    <tr>
                                        <td>Delegacje</td>
                                        <td>' . $salaryInfo['trips'] . ' x ' . $rates->trip_rate . '</td>
                                        <td>' . ($salaryInfo['trips'] * $rates->trip_rate) . '</td>
                                    </tr>
                                    <tr>
                                        <td colspan="2">Łącznie: ' . ($salaryInfo['all'] + $salaryInfo['trips']) . '</td><td>' . $total . '</td>
                                    </tr>
                                </tbody>
                            </table>';
                $this->view .= '<br />' .  $table . '<br />';
                $buttons = array();
                $buttons[] = array('action'=>'','text'=>'Powrót');
                $this->view .= Menu::createMenu($buttons, $params, false, array(), true);
            break;
        }
    }
}
?>
